﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using DTO;
using Telerik.Web.UI;

namespace TVC_WEB.Global
{
    public partial class cart : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["cate"] != null)
                {
                    lbCatePath.Text = GetCategoryPath(Session["cate"].ToString());
                }
                LoadCart();
            }
        }

        private void LoadCart()
        {
            if (Session["cart"] != null)
            {
                List<CartInfo> lcart = Session["cart"] as List<CartInfo>;
                BUS.ProductsControl ctr = new BUS.ProductsControl();
                DataTable dt = ctr.LoadCart(lcart, ref sErr);
                lvcart.DataSource = dt;
                lvcart.DataBind();
                lbTotal.Text = GetPrice(dt.Compute("SUM(SubTotal)", "").ToString());
            }
        }

        public string GetImage(string imageName)
        {
            string strUploadFolder = "/" + ConfigurationManager.AppSettings["UploadFolder"].ToString() + "Images/";
            return strUploadFolder + imageName;
        }
        public string GetPrice(string imageName)
        {
            if (imageName != "" && imageName != null)
                return Convert.ToDouble(imageName).ToString("N0") + "đ";
            else return "";
        }
        public string GetCategoryPath(string cate)
        {
            string result = "";//"<a href=\"/\">Home</a>&nbsp;&nbsp;";
            string[] arrCare = cate.Split(',');
            BUS.CategoriesControl ct = new BUS.CategoriesControl();
            for (int i = 0; i < arrCare.Length; i++)
            {
                DTO.CategoriesInfo inf = ct.Get(Convert.ToInt32(arrCare[i]), ref sErr);
                if (inf.Type == "Category")
                {
                    DataTable dt = ct.GetAllParents(inf.ID, ref sErr);
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        DataRow row = dt.Rows[j];
                        if (j == 0)
                            result = "<a href='prolist.aspx?cate=" + row["ID"].ToString() + "&catetype=Category'><b>" + row["NameVN"].ToString() + "</b></a>" + result;
                        else
                            result = "<a href='prolist.aspx?cate=" + row["ID"].ToString() + "&catetype=Category'>" + row["NameVN"].ToString() + "</a> &nbsp;&nbsp;&gt;&nbsp;&nbsp;" + result;
                    }
                    return "<a href='/Global/'>Home</a>&nbsp;&nbsp;&gt; &nbsp;&nbsp;" + result;

                }
            }
            return "<a href='/Global/'>Home</a>";
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateCart();
            //LoadCart();
        }

        private void UpdateCart()
        {
            if (Session["cart"] != null)
            {
                List<CartInfo> lcart = Session["cart"] as List<CartInfo>;
                foreach (ListViewDataItem item in lvcart.Items)
                {
                    string id = ((HiddenField)item.FindControl("hdID")).Value;
                    string qty = ((TextBox)item.FindControl("txtQty")).Text;
                    int val;
                    bool flag = Int32.TryParse(qty, out val);
                    if (flag)
                        foreach (CartInfo x in lcart)
                        {
                            if (x.ProductID.ToString() == id)
                            {
                                x.Qty = val;
                            }
                        }
                }
                Session["cart"] = lcart;
                LoadCart();
            }
        }

        protected void btnShop_Click(object sender, EventArgs e)
        {
            if (Session["currentshop"] != null)
                Response.Redirect(Session["currentshop"].ToString());
        }

        protected void btnCheckout_Click(object sender, EventArgs e)
        {
            Response.Redirect("Order.aspx");
        }

        

        protected void lvcart_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            string id = ((HiddenField)lvcart.Items[e.ItemIndex].FindControl("hdID")).Value;
            if (Session["cart"] != null)
            {
                List<CartInfo> lcart = Session["cart"] as List<CartInfo>;
                int flag = -1;
                for (int i=0;i<lcart.Count;i++)
                {
                    CartInfo x = lcart[i];
                    if (x.ProductID.ToString() == id)
                    {
                        flag = i;break;
                    }
                }
                if (flag != -1)
                    lcart.RemoveAt(flag);
                Session["cart"] = lcart;
                LoadCart();
            }
        }
    }
}
